package com.chengliang.zuoye.controller;

import com.chengliang.zuoye.bean.Menu;
import com.chengliang.zuoye.response.JsonResult;
import com.chengliang.zuoye.service.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Author chengliang
 * @Date 2019-06-17 20:37
 * @Description //TODO
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    private Logger logger= LoggerFactory.getLogger(getClass());

    private final MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }


    /**
     * 修改菜单信息
     * @param menu 修改后的菜单信息
     * @return 处理结果
     */
    @PostMapping("/update")
    @ResponseBody
    public Object update(Menu menu){
        logger.debug("修改菜单信息"+menu);
        return JsonResult.is(menuService.updateMenuById(menu),"修改菜单信息");
    }
    /**
     * 修改页面
     * @return
     */
    @GetMapping("/info/{id}")
    public String info(@PathVariable Integer id,Model model){
        model.addAttribute("menu",menuService.getMenuById(id));
        model.addAttribute("menuList",menuService.onlyOneMenuList());
        logger.debug("菜单ID："+id);
        return "menu/update";
    }

    @PostMapping("/add")
    @ResponseBody
    public Object add(Menu menu){
        logger.debug("添加菜单"+menu);
        return JsonResult.is(menuService.saveMenu(menu),"添加菜单");
    }

    @GetMapping("/add")
    public String addPage(Model model){
        model.addAttribute("menuList",menuService.onlyOneMenuList());
        return "menu/add";
    }

    @PostMapping("/status/change")
    @ResponseBody
    public Object changeStatus(Integer id,Boolean status){
        return JsonResult.is(menuService.changeStatus(id,status),!status?"启用":"禁用");
    }

    @GetMapping("/index")
    public String index(){
        return "menu/index";
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public Object delete(@PathVariable Integer id){
        return JsonResult.is(menuService.dropMenuById(id),"删除菜单");
    }


    @GetMapping("/data")
    @ResponseBody
    public Object json(){
        return menuService.menuList();
    }

    @GetMapping("/data/json")
    @ResponseBody
    public Object jsonResult(){
        return JsonResult.ok(menuService.oneMenuList());
    }

}
